package com.config;

import com.service.impl.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Configurable;
import org.apache.shiro.realm.Realm;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    //自定义认证对象
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    //认证器
    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm());
        return securityManager;
    }

    //过滤工厂
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //添加安全认证
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        //认证失败后转跳页面
        shiroFilterFactoryBean.setLoginUrl("/signIn");

        //授权失败后转跳页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/errors");

        //拦截规则
        Map<String, String> map = new HashMap<String,String>();
        /*map.put("/**","authc");
        map.put("/doLogin","anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);*/
        return shiroFilterFactoryBean;
    }
}
